package com.yuan.model_convenient.service.impl;

import com.yuan.model_convenient.domain.ConvenientActivityEnroll;
import com.yuan.model_convenient.repository.ConvenientActivityEnrollDao;
import com.yuan.model_convenient.service.ConvenientActivityEnrollService;
import com.yuan.model_convenient.service.dto.ConvenientActivityEnrollDto;
import com.yuan.model_convenient.service.dto.ConvenientActivityEnrollQueryCriteria;
import com.yuan.model_convenient.service.mapstruct.ConvenientActivityEnrolMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
public class ConvenientActivityEnrollServiceImpl implements ConvenientActivityEnrollService {

    private final ConvenientActivityEnrollDao convenientActivityEnrollDao;

    private final ConvenientActivityEnrolMapper convenientActivityEnrolMapper;

    @Override
    public PageResult<ConvenientActivityEnrollDto> queryAllByType(ConvenientActivityEnrollQueryCriteria criteria, Pageable pageable) {
        Page<ConvenientActivityEnroll> all = convenientActivityEnrollDao.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(all.map(convenientActivityEnrolMapper::toDto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ConvenientActivityEnroll resources) {
        ConvenientActivityEnroll convenientActivityEnrollByTwoId = convenientActivityEnrollDao.findConvenientActivityEnrollByTwoId(resources.getActivityId(), resources.getUserId());
        if (convenientActivityEnrollByTwoId == null) {
            convenientActivityEnrollDao.save(resources);
        } else {
            convenientActivityEnrollByTwoId.setActivityId(resources.getActivityId());
            convenientActivityEnrollByTwoId.setUserId(resources.getUserId());
            convenientActivityEnrollDao.save(convenientActivityEnrollByTwoId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ConvenientActivityEnroll resources) throws Exception {
        Optional<ConvenientActivityEnroll> byId = convenientActivityEnrollDao.findById(resources.getId());
        if (byId.isPresent()) {
            ConvenientActivityEnroll convenientActivityEnroll = byId.get();
            convenientActivityEnroll.setUserId(resources.getUserId());
            convenientActivityEnroll.setActivityId(resources.getActivityId());
            convenientActivityEnrollDao.save(convenientActivityEnroll);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer ids) {
        convenientActivityEnrollDao.deleteById(ids);
    }
}
